package agenda;


public class DayEvent extends Entry implements Occupiable {
	
	/*** Constructors */
	protected DayEvent(Agenda<?> agenda, int day, String subject,
			boolean isOccupying) {
		super(agenda, day, subject);
		setOccupying(isOccupying);
		agenda.addEntry(this);
	}
	
	public DayEvent(int day, Agenda<? super DayEvent> agenda, String subject, boolean isOccupying){
		this(agenda, day, subject, isOccupying);
	}

	
	/*** Occupying */
	@Override
	public boolean isOccupying() {
		return this.isOccupying;
	}

	@Override
	public void setOccupying(boolean isOccupying) {
		if (!this.isTerminated())
			this.isOccupying = isOccupying;
	}
	
	@Override
	public void turnOccupyingOn() {
		setOccupying(true);
	}
	
	@Override
	public void turnOccupyingOff() {
		setOccupying(false);
	}

	private boolean isOccupying;

	
	/*** Timeable */
	@Override
	public long getEndTime() {
		return this.getStartTime() + 24*60*60*1000;
	}

	
	/*** General purpose */
	@Override
	public boolean overlapsWith(Entry other) {
		if ( (other == null) || (! this.isOccupying()) )
			return false;
		if (this.getDay() != other.getDay())
			return false;
		if (Occupiable.class.isInstance(other))
			return (((Occupiable) other).isOccupying());
		return true;
	}

	@Override
	public DayEvent mergeWith(Entry other) {
		assert (other != null);
		assert (this.getDay() == other.getDay());
		assert (! this.isTerminated()) && (! other.isTerminated());
		assert (this.getAgenda() == other.getAgenda());
		assert (other instanceof DayEvent);
		DayEvent otherDayEvent = (DayEvent) other;
		this.setSubject(this.getSubject() + other.getSubject());
		this.setOccupying(this.isOccupying() || otherDayEvent.isOccupying());
		other.terminate();
		return this;
	}
}
